﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

/// <summary>
/// Summary description for UserAccount
/// </summary>
public class UserAccount
{
    public int accountID { set; get; }
    public String branchCode { set; get; }
    public String accountTypeCode { set; get; }
    public int accountNumber { set; get; }
    public String currency { set; get; }
    public String accountType { set; get; }
    public int userId { set; get; }

	public UserAccount()
	{
    
	}


    public static UserAccount getUserAccountByNumber(String number){

        UserAccount userAccount = new UserAccount();
        
        var conn = DBManager.getSqlConnection();
        conn.Open();

        String[] tokens = number.Split('-');
        userAccount.branchCode = tokens[0];
        userAccount.accountNumber = Convert.ToInt32(tokens[1]);
        userAccount.accountTypeCode = tokens[2];

        string sql = "select accountID,branchCode,accountNumber,accountTypeCode, accountType, currency, userID from Account where branchCode = '" + userAccount.branchCode + "' and accountNumber= " + userAccount.accountNumber + " and accountTypeCode= '" + userAccount.accountTypeCode + "'";
        var cmd = new SqlCommand(sql, conn);

        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            userAccount.accountID = reader.GetInt32(0);
            userAccount.branchCode = reader.GetString(1);
            userAccount.accountNumber = reader.GetInt32(2);
            userAccount.accountTypeCode = reader.GetString(3);
            userAccount.accountType = reader.GetString(4);
            userAccount.currency = reader.GetString(5);
            userAccount.userId = reader.GetInt32(6);
        }

        reader.Close();

        conn.Close();


        return userAccount;
    }

    public static UserAccount getUserAccountByAccountId(int accountId)
    {

        UserAccount userAccount = new UserAccount();

        var conn = DBManager.getSqlConnection();
        conn.Open();

        string sql = "select accountID,branchCode,accountNumber,accountTypeCode, accountType, currency, userID from Account where accountID=" + accountId;
        var cmd = new SqlCommand(sql, conn);

        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            userAccount.accountID = reader.GetInt32(0);
            userAccount.branchCode = reader.GetString(1);
            userAccount.accountNumber = reader.GetInt32(2);
            userAccount.accountTypeCode = reader.GetString(3);
            userAccount.accountType = reader.GetString(4);
            userAccount.currency = reader.GetString(5);
            userAccount.userId = reader.GetInt32(6);
        }

        reader.Close();

        conn.Close();


        return userAccount;
    }



}